/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    2012 year
 *    Author: Mikhail Khromov aka MikeDM (Mike D'Morto)
 *    E-Mail: mikedmorto@gmail.com
 */

#include "diawait.h"
#include "ui_diawait.h"
#include "base.h"

DiaWait::DiaWait(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DiaWait)
{
    ui->setupUi(this);
    this->isTimeout=false;
    connect(&this->timer,SIGNAL(timeout()),this,SLOT(slotTik()));
    this->ui->label->setWordWrap(true);
}

DiaWait::~DiaWait(){

}

int DiaWait::start(int msec, const QString &title, const QString &text){
    // отключаем второй лабел
    this->ui->lbl_stage->hide();
    this->time = msec;
    this->ui->progressBar->setMinimum(0);
    this->ui->progressBar->setMaximum(this->time);
    this->ui->progressBar->setValue(this->time);
    this->ui->progressBar_stages->hide();
    this->setWindowTitle(title);
    this->ui->label->setText(text);
    this->timer.start(100);
    this->isTimeout=false;
    if(this->isUnhided){
        this->isRet = false;
        for(;;){
            Sleeper::msleep(100);
            QApplication::processEvents(QEventLoop::AllEvents);
            if(this->isRet){
                return this->ret;
            }
        }
    }else{
        return this->exec();
    }
}

// режим работы с этапами, без таймаута.
int DiaWait::start(const QString &title, const QString &text, const QString &stage){
    this->ui->label->setText(text);
    this->ui->lbl_stage->show();
    this->ui->lbl_stage->setText(stage);
    this->ui->progressBar->setMinimum(0);
    this->ui->progressBar->setMaximum(10);
    this->ui->progressBar->setValue(0);
    this->ui->progressBar_stages->hide();
    this->setWindowTitle(title);
    this->isUnhided = false;
    return this->exec();
}



void DiaWait::slotUpdateStatus(const QString &text, qint64 value, qint64 max){
    this->ui->lbl_stage->setText(text);
    this->ui->progressBar->setValue(value);
    this->ui->progressBar->setMaximum(max);
}

void DiaWait::slotStopOk(){
    // останавливаем снаружи по событию.
    if(this->isUnhided){
        this->timer.stop();
        this->ui->progressBar->setValue(0);
        this->ret = QDialog::Accepted;
        this->isRet = true;
    }else{
        this->timer.stop();
        this->accept();
    }
}

void DiaWait::slotStopError(){
    // останавливаем аварийно снаружи
    if(this->isUnhided){
        this->timer.stop();
        this->ui->progressBar->setValue(0);
        this->ret = QDialog::Rejected;
        this->isRet = true;
    }else{
        this->timer.stop();
        this->reject();
    }

}

void DiaWait::slotTik(){
    // считаем таймаут ли ... ну и прогресс бар рисуем =)
    this->time-=100;
    if(this->time <= 0){
        // таймаут
        if(this->isUnhided){
            this->ret = QDialog::Rejected;
            this->isRet = true;
        }else{
            this->isTimeout=true;
            this->reject();
        }
    }else{
        this->ui->progressBar->setValue(this->time);
    }
}

bool DiaWait::isTimeouted(){
    return this->isTimeout;
}

void DiaWait::setUnhide(bool state){
    this->isUnhided = state;
    if(state){
        this->show();
    }
}

void DiaWait::setHided(){
    this->isUnhided = false;
    this->hide();
}

void DiaWait::slotPreReject(){
    if(this->isUnhided){
        this->timer.stop();
        this->ui->progressBar->setValue(0);
        this->ret = QDialog::Rejected;
        this->isRet = true;
    }else{
        this->timer.stop();
        this->reject();
    }
}


// перехватываем закрытие диалога
void DiaWait::closeEvent(QCloseEvent * event){
    // если не главная вкладка то возвращаем на главную
    if(this->isUnhided){
        this->timer.stop();
        this->ui->progressBar->setValue(0);
        this->ret = QDialog::Rejected;
        this->isRet = true;
        event->ignore();
    }else{
        this->timer.stop();
        event->accept();
    }


}
void DiaWait::keyPressEvent(QKeyEvent *e){
    // перехватываем esc клавишу закрытия диалога.
    if (e->key() == Qt::Key_Escape)
        {
        if(this->isUnhided){
            this->timer.stop();
            this->ui->progressBar->setValue(0);
            this->ret = QDialog::Rejected;
            this->isRet = true;
            e->ignore();
        }else{
            this->timer.stop();
            QDialog::keyPressEvent(e);
        }

    }else{

        QDialog::keyPressEvent(e);
    }
}
